from py2neo import Graph, Node, Relationship
import pandas as pd


# ===================== 导入 =====================
# 连接到本地运行的 Neo4j 实例
graph = Graph("bolt://localhost:7687", auth=("neo4j", "password"))

# 从CSV文件加载数据
df = pd.read_csv('people.csv')

# 创建节点
for index, row in df.iterrows():
    person = Node("Person", name=row['name'], age=row['age'], city=row['city'])
    graph.create(person)

print("Data imported successfully.")


# ===================== 导出  =====================

# 创建连接
graph = Graph("bolt://localhost:7687", auth=("neo4j", "password"))

# 查询数据
query = """
MATCH (p:Person)
RETURN p.name AS name, p.age AS age, p.city AS city
"""

# 执行查询并将结果转换为 DataFrame
result = graph.run(query).data()
people_data = [record for record in result]
df = pd.DataFrame(people_data)

# 导出为 CSV 文件
df.to_csv('exported_people.csv', index=False)

print("Data exported successfully.")
